package org.racsor.cc.action;

import java.util.Date;
import java.util.Map;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.racsor.cc.model.PMF;
import org.racsor.cc.model.RequestCC;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class SaveRequest extends HttpServlet {
    private static final Logger log = Logger.getLogger(SaveRequest.class.getName());
    private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

    public void doGet(HttpServletRequest req, HttpServletResponse resp) {

	try {
	    String url = (String) req.getParameter("url");
	    log.warning("url:" + url);

	    UserService userService = UserServiceFactory.getUserService();
	    User user = userService.getCurrentUser();

	    Date date = new Date();
	    RequestCC greeting = new RequestCC(user, url, date);

	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    try {
		pm.makePersistent(greeting);
	    } finally {
		pm.close();
	    }

	    RequestDispatcher dispatcher = req.getRequestDispatcher("WEB-INF/jsp/requestcc.jsp");
	    dispatcher.forward(req, resp);

	} catch (Exception e) {
	    log.warning(ExceptionUtils.getStackTrace(e));
	}

    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp) {

	try {
	    String url = (String) req.getParameter("url");
	    String parameters = (String) req.getParameter("parameters");
	    String frecuency = (String) req.getParameter("frecuency");
	    log.warning("doPost:url:" + url);

	    UserService userService = UserServiceFactory.getUserService();
	    User user = userService.getCurrentUser();

	    Date date = new Date();
	    Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
	    BlobKey blobKey = blobs.get("myFile");
	    BlobInfoFactory blobInfoFactory = new BlobInfoFactory();
	    BlobInfo blobInfo = blobInfoFactory.loadBlobInfo(blobKey);

	    String contentType = blobInfo.getContentType();
	    long size = blobInfo.getSize();
	    Date creation = blobInfo.getCreation();
	    String fileName = blobInfo.getFilename();
	    
	    RequestCC requestcc = new RequestCC(user, url, date);
	    requestcc.setFrecuency(frecuency);
	    requestcc.setParameters(parameters);
	    requestcc.setBlob(blobKey);
	    requestcc.setContentType(contentType);
	    requestcc.setFileName(fileName);
	    requestcc.setSize(size);
	    log.warning("doPost:blobKey:" + blobKey);
	    
//	     blobstoreService.serve(blobKey, resp);


	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    try {
		pm.makePersistent(requestcc);
	    } finally {
		pm.close();
	    }

	    log.warning("doPost:id:" + requestcc.getId());
	    log.warning("doPost::" + requestcc);
	    RequestDispatcher dispatcher = req.getRequestDispatcher("WEB-INF/jsp/requestcc.jsp");
	    dispatcher.forward(req, resp);

	} catch (Exception e) {
	    log.warning(ExceptionUtils.getStackTrace(e));
	}

    }
}
